package com.ld.wms.business.material.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ld.wms.business.material.mapper.FieldInfoMapper;
import com.ld.wms.business.material.mapper.MaterialTypeFieldMapper;
import com.ld.wms.business.material.mapper.MaterialTypeMapper;
import com.ld.wms.business.material.model.MaterialTypeFieldModel;
import com.ld.wms.business.material.model.MaterialTypeModel;
import com.ld.wms.business.material.service.MaterialTypeService;
import com.ld.wms.business.material.vo.req.MaterialTypeFieldReqVo;
import com.ld.wms.business.material.vo.req.MaterialTypeReqVo;
import com.ld.wms.business.material.vo.resp.FieldInfoRespVo;
import com.ld.wms.business.material.vo.resp.MaterialTypeFieldRespVo;
import com.ld.wms.business.material.vo.resp.MaterialTypeRespVo;
import com.ld.wms.common.vo.GeneratePageListVo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 物料类型数据 服务实现类
 * </p>
 *
 * @author wangfeng
 * @since 2025-02-24
 */
@Service
public class MaterialTypeServiceImpl extends ServiceImpl<MaterialTypeMapper, MaterialTypeModel> implements MaterialTypeService {

    @Autowired
    private MaterialTypeMapper materialTypeMapper;

    @Autowired
    private MaterialTypeFieldMapper materialTypeFieldMapper;

    @Autowired
    private FieldInfoMapper fieldInfoMapper;


    @Override
    public GeneratePageListVo<MaterialTypeRespVo> getPageList(Integer current, Integer size) {
        IPage<MaterialTypeRespVo> iPage = new Page<>(current, size);
        IPage<MaterialTypeRespVo> iPage1 = materialTypeMapper.selectPageList(iPage);
        return new GeneratePageListVo<>(iPage1);
    }

    @Override
    public Map<Long, MaterialTypeModel> getAllMap() {
        Map<Long, MaterialTypeModel> map = new HashMap<>();
        List<MaterialTypeModel> materialTypeModelList = materialTypeMapper.selectList(null);
        for (MaterialTypeModel materialTypeModel : materialTypeModelList) {
            map.put(materialTypeModel.getId(), materialTypeModel);
        }
        return map;
    }

    @Override
    public void add(MaterialTypeReqVo reqVo) {
        MaterialTypeModel materialTypeModel = new MaterialTypeModel();
        BeanUtils.copyProperties(reqVo, materialTypeModel);
        materialTypeMapper.insert(materialTypeModel);
    }

    @Override
    public void updateById(MaterialTypeReqVo reqVo) {
        MaterialTypeModel materialTypeModel = new MaterialTypeModel();
        BeanUtils.copyProperties(reqVo, materialTypeModel);
        materialTypeMapper.updateById(materialTypeModel);
    }

    @Override
    public void configMaterialTypeField(MaterialTypeFieldReqVo reqVo) {
        // 删除原来的配置信息
        Map<String, Object> columnMap = new HashMap<String, Object>();
        columnMap.put(MaterialTypeFieldModel.MATERIAL_TYPE_ID, reqVo.getMaterialTypeId());
        materialTypeFieldMapper.deleteByMap(columnMap);
        // 新增配置信息
        MaterialTypeFieldModel materialTypeFieldModel = null;
        List<Long> fieldInfoIdList = reqVo.getFieldInfoIdList();
        for (Long fieldInfoId : fieldInfoIdList) {
            materialTypeFieldModel = new MaterialTypeFieldModel();
            materialTypeFieldModel.setMaterialTypeId(reqVo.getMaterialTypeId());
            materialTypeFieldModel.setFieldInfoId(fieldInfoId);
            materialTypeFieldMapper.insert(materialTypeFieldModel);
        }
    }

    @Override
    public MaterialTypeFieldRespVo getMaterialTypeFieldDetails(Long materialTypeId) {
        MaterialTypeFieldRespVo materialTypeFieldRespVo = new MaterialTypeFieldRespVo();
        QueryWrapper<MaterialTypeFieldModel> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq(MaterialTypeFieldModel.MATERIAL_TYPE_ID, materialTypeId);
        List<FieldInfoRespVo> fieldInfoRespVos = fieldInfoMapper.selectListByMaterialTypeId(materialTypeId);
        materialTypeFieldRespVo.setFieldInfoRespVoList(fieldInfoRespVos);
        materialTypeFieldRespVo.setMaterialTypeId(materialTypeId);
        return materialTypeFieldRespVo;
    }
}
